#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################

# 检查是否需要构建 libcurl 文档
# 若 BUILD_LIBCURL_DOCS 为 TRUE，则执行以下操作
if(BUILD_LIBCURL_DOCS)
  # 添加 libcurl 子目录，CMake 会进入该子目录继续处理其 CMakeLists.txt 文件
  add_subdirectory(libcurl)
endif()

# 检查是否启用构建 curl 手册页且需要构建 curl 可执行文件
# 若 ENABLE_CURL_MANUAL 和 BUILD_CURL_EXE 都为 TRUE，则执行以下操作
if(ENABLE_CURL_MANUAL AND BUILD_CURL_EXE)
  # 添加 cmdline-opts 子目录，CMake 会进入该子目录继续处理其 CMakeLists.txt 文件
  add_subdirectory(cmdline-opts)
endif()

# 检查是否需要构建其他杂项文档
# 若 BUILD_MISC_DOCS 为 TRUE，则执行以下操作
if(BUILD_MISC_DOCS)
  # 遍历需要生成手册页的杂项文档列表
  foreach(_man_misc IN ITEMS "curl-config" "mk-ca-bundle")
    # 设置生成的手册页文件的路径
    set(_man_target "${CMAKE_CURRENT_BINARY_DIR}/${_man_misc}.1")
    # 添加自定义命令，用于生成手册页文件
    add_custom_command(
      # 命令的输出文件为生成的手册页文件
      OUTPUT "${_man_target}"
      # 设置命令执行的工作目录为当前源文件目录
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      # 执行 Perl 脚本 cd2nroff 将 Markdown 文件转换为 nroff 格式的手册页文件
      COMMAND "${PERL_EXECUTABLE}" "${PROJECT_SOURCE_DIR}/scripts/cd2nroff" "${_man_misc}.md" > "${_man_target}"
      # 该命令依赖于 cd2nroff 脚本和对应的 Markdown 文件
      DEPENDS "${PROJECT_SOURCE_DIR}/scripts/cd2nroff" "${_man_misc}.md"
      # 确保命令参数按原样传递
      VERBATIM
    )
    # 添加自定义目标，确保每次构建时都会生成对应的手册页文件
    add_custom_target("curl-generate-${_man_misc}.1" ALL DEPENDS "${_man_target}")
    # 检查是否禁用安装操作
    if(NOT CURL_DISABLE_INSTALL)
      # 若未禁用安装，则将生成的手册页文件安装到指定的 man1 目录
      install(FILES "${_man_target}" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
    endif()
  endforeach()
endif()
